package com.example.xt.controller;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xt.common.Constants;
import com.example.xt.common.Result;
import com.example.xt.entity.Medicine;
import com.example.xt.mapper.MedicineMapper;
import com.example.xt.service.IMedicineService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.sql.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-02-27
 */
@RestController
@RequestMapping("/medicine")
public class MedicineController {
    @Resource
    private IMedicineService medicineService;
    @Resource
    private MedicineMapper medicineMapper;

    @PostMapping("/save")
    public Result insert(@RequestBody Medicine medicion) {
        String name = medicion.getName();
        Integer costprice = medicion.getCostprice();
        Integer price = medicion.getPrice();
        Integer stock = medicion.getStock();
        Integer ep = medicion.getEp();
        Date ex = medicion.getEx();
        String clas = medicion.getClas();
        String region = medicion.getRegion();
        if (name != null && stock != null && costprice != null && price != null && ex != null && ep != null && clas != null && region != null) {

            if (null == medicineService.or(name, costprice, price, ep, ex)) {//查不到则新增
                return Result.success(medicineService.save(medicion));
            } else {
                if (null == medicineService.addstock(name, costprice, price, ep, ex, stock, clas, region)) {//除了库存以外还有不同则更新
                    return Result.success(medicineService.saveOrUpdate(medicion));
                } else {
                    return Result.success(medicineService.OrUpdate(stock, medicineService.or(name, costprice, price, ep, ex)));//否则根据id增加库存
                }
            }
        } else {
            return Result.error(Constants.CODE_400, "数据不能为空");
        }
    }

    @PostMapping("/updata")
    public Result Updata(@RequestBody Medicine medicine) {
        String name = medicine.getName();
        Integer costprice = medicine.getCostprice();
        Integer price = medicine.getPrice();
        Integer stock = medicine.getStock();
        Integer ep = medicine.getEp();
        Date ex = medicine.getEx();
        String clas = medicine.getClas();
        String region = medicine.getRegion();
        if (name != null && stock != null && costprice != null && price != null && ex != null && ep != null && clas != null && region != null) {
            return Result.success(medicineService.saveOrUpdate(medicine));
        } else {
            return Result.error(Constants.CODE_400, "数据不能为空");
        }
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) { // [1,2,3]
        return Result.success(medicineService.removeByIds(ids));
    }

    /**
     * excel 导入
        * 批量新增
        *
        * @param file
        * @throws Exception
        */
       @PostMapping("/import")
       public Boolean imp(MultipartFile file) throws Exception {
              InputStream inputStream = file.getInputStream();
              ExcelReader reader = ExcelUtil.getReader(inputStream);
              List<Medicine> list = reader.readAll(Medicine.class);
              medicineService.saveBatch(list);
              return true;
       }
       @DeleteMapping("/{id}")
       public boolean delete(@PathVariable Integer id) {
              return medicineService.removeById(id);
       }
       @GetMapping("/{id}")
       public Medicine findOne(@PathVariable Integer id) {
              return medicineService.getById(id);
        }
       @GetMapping("/page")//查询未过期的
       public Page findPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam (defaultValue = "")String pagename) {
           medicineMapper.exp();
              QueryWrapper queryWrapper= new QueryWrapper();
              queryWrapper.orderByDesc("id");//根据ID倒序
              if (!"".equals(pagename)){
                     queryWrapper.like("name", pagename);//根据名称查
              }
              queryWrapper.inSql("id","select id from medicine where ep*30-exp > 0");
              return medicineService.page(new Page<>(pageNum, pageSize), queryWrapper);
        }
       @GetMapping("/page/expire")//查询已经过期的
       public Page<Medicine> findPage(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize
                                     ) {
           medicineMapper.exp();
              QueryWrapper queryWrapper= new QueryWrapper();
              queryWrapper.orderByDesc("id");//根据ID倒序

              queryWrapper.inSql("id","select id from medicine where (ep*30-exp) < 1");

              return medicineService.page(new Page<>(pageNum, pageSize), queryWrapper);
       }

}

